﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Timers;
using System.Xml.Serialization;
using library;

namespace Missile_System_Control_Center
{
    public partial class Form1 : Form
    {
        static string cwdd = System.IO.Directory.GetCurrentDirectory();
        string cwd = System.IO.Directory.GetDirectoryRoot(cwdd);
        static System.Windows.Forms.Timer loadTimer = new System.Windows.Forms.Timer();
        static System.Windows.Forms.Timer decTimer = new System.Windows.Forms.Timer();
        double counter;
        Boolean firstTime = true;
        static double distanceToTarget;
        static string outcome;

        public Form1()
        {
            InitializeComponent();
        }

        private void boxTarget_Enter(object sender, EventArgs e)
        {
            boxTarget.Text = "Target Information";
        }

        private void boxMissle_Enter(object sender, EventArgs e)
        {
            boxMissle.Text = "Missle Inventory";
        }

        private void abortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSerial_Click(object sender, EventArgs e)
        {
            lblTarget.Visible = true;
            lblPerc.Visible = true;
            lblDistance.Visible = true;
            boxDist.Visible = true;
            boxETA.Visible = true;
            boxName.Visible = true;
            lblStatus.Visible = true;
            lblNoTarg.Visible = false;

            cwd = cwd.Replace("\\bin\\Debug", "");

            MissileLaunch COMM_Event = new MissileLaunch();

            try
            {
                XmlReader reader = new XmlTextReader(cwd + "\\ProjectFiles\\COMM_Event.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(MissileLaunch));
                COMM_Event = (MissileLaunch)serializer.Deserialize(reader);
            }
            catch (Exception ex)
            {
                //Write ex to Log
            }

            lblStatus.Text = "Missile Launching";

            boxName.Text = COMM_Event.Targ;
            boxDist.Text = COMM_Event.Distance;
        }

        private void btnDe_Click(object sender, EventArgs e)
        {
            //myTimer.Tick += new EventHandler(decreaseDistance);  //Method timerRun to be run
            //myTimer.Interval = 1000;   //Sets the event to happen every second
            //myTimer.Start();    //Starts the timer event
        } 

        private void Form1_Load(object sender, EventArgs e)
        {
            cwd += "C#\\MD Final\\Files\\";

            lblTarget.Visible = false;
            lblPerc.Visible = false;
            lblDistance.Visible = false;
            boxDist.Visible = false;
            boxETA.Visible = false;
            boxName.Visible = false;
            lblStatus.Visible = false;

            boxNukeL.Text = Missilecount.Nuke.ToString();
            boxJavL.Text = Missilecount.Javelin.ToString();
            boxTomaL.Text = Missilecount.Tomahawk.ToString();
            boxBunkL.Text = Missilecount.BunkerBuster.ToString();

            Status MIS_Status = new Status();

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Status));
                TextWriter textWriter = new StreamWriter(cwd + "MIS_Status.xml");
                serializer.Serialize(textWriter, MIS_Status);
                textWriter.Close();
            }
            catch (Exception ex)
            {
                //write to the array ErrorLog
            }

            loadTimer.Tick += new EventHandler(timerRun);
            loadTimer.Interval = 1000;
            loadTimer.Start();
        }

        private void timerRun(object source, EventArgs myEventArgs)
        {
            loadTimer.Stop();
            MissileLaunch COMM_Event = new MissileLaunch();

            try
            {
                XmlReader reader = new XmlTextReader(cwd + "COMM_Event.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(MissileLaunch));
                COMM_Event = (MissileLaunch)serializer.Deserialize(reader);
                reader.Close();
                lblNoTarg.Visible = false;
                lblTarget.Visible = true;
                lblDistance.Visible = true;
                lblPerc.Visible = true;
                boxName.Visible = true;
                boxDist.Visible = true;
                boxETA.Visible = true;

                boxName.Text = COMM_Event.Targ;
                boxDist.Text = COMM_Event.Distance;
                distanceToTarget = Convert.ToDouble(COMM_Event.Distance);

                if (firstTime)
                {
                    outcome = findHitMiss(Convert.ToInt32(COMM_Event.Percentage));
                    firstTime = false;
                }

                decreaseTime();
            }

            catch (Exception ex)
            {
                //errorLog[errorCheck].
            }

            loadTimer.Start();
        }

        private void decreaseTime ()
        {
            if (distanceToTarget.Equals(0))
            {
                writeBack();
            }
            decTimer.Tick += new EventHandler(decreaseDist);
            decTimer.Interval = 1000;
            decTimer.Start();
        }

        private void decreaseDist(object source, EventArgs myEventArgs)
        {
            decTimer.Stop();
            distanceToTarget -= 100;
            boxDist.Text = distanceToTarget.ToString();
            decTimer.Start();
        }

        private string findHitMiss(int percentage)
        {
            int numRand = 100 - percentage;
            int[]randomNumbers;
            int redo = 0;
            string outcome;
            randomNumbers = new int[numRand];
            for (int k = 0; k < numRand; k++)
            {
                randomNumbers[k] = library.Utility.RandomNumber(1, 50);
                for (int l = 0; l < randomNumbers.Length; l++)
                {
                    if (randomNumbers[k].Equals(randomNumbers[l]))
                    {
                        redo++;
                    } 
                }
            }
            if (redo > (numRand/2))
            {
                outcome = "Miss";
            }
            else
                outcome = "Hit";
            return outcome;
        }

        private void writeBack()
        {
            Contact MIS_Event = new Contact();

            try
            {
                XmlReader reader = new XmlTextReader(cwd + "MIS_Event.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Contact));
                MIS_Event = (Contact)serializer.Deserialize(reader);
                reader.Close();

                if (outcome.Equals("Hit"))
                {
                    MIS_Event.Outcome = Contact.StatusList.Hit;
                }
                if (outcome.Equals("Miss"))
                {
                    MIS_Event.Outcome = Contact.StatusList.Miss;
                }
                if (outcome.Equals("Abort"))
                {
                    MIS_Event.Outcome = Contact.StatusList.SelfDestruct;
                }

                lblNoTarg.Visible = true;
                lblTarget.Visible = false;
                lblDistance.Visible = false;
                lblPerc.Visible = false;
                boxName.Visible = false;
                boxDist.Visible = false;
                boxETA.Visible = false;

                try
                {
                    string path2 = cwd + "COMM_Event.xml";
                    File.Delete(path2);
                    loadTimer.Start();
                }
                catch (Exception ex)
                {
                    //load 
                }
            }

            catch (Exception ex)
            {
                //errorLog[errorCheck].
            }

        }

        private void Radar_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                string path2 = cwd + "MIS_Status.xml";
                File.Delete(path2);
            }
            catch (Exception ex)
            {
                //Errorlog
            }
        }

        
    }
}
